<?php
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */
use App\Common\DbSelector;
use App\Process\MonitorProcess;
use Swoft\Crontab\Process\CrontabProcess;
use Swoft\Db\Pool;
use Swoft\Http\Server\HttpServer;
use Swoft\Task\Swoole\SyncTaskListener;
use Swoft\Task\Swoole\TaskListener;
use Swoft\Task\Swoole\FinishListener;
use Swoft\Rpc\Server\ServiceServer;
use Swoft\WebSocket\Server\WebSocketServer;
use Swoft\Server\SwooleEvent;
use Swoft\Db\Database;
use Swoft\Redis\RedisDb;

return [
    'config'   => [
        'path' => __DIR__ . '/../config',
        'env' => 'dev'
    ],
    'consul' => [
        'host'      => config('consul.HOST'),
        'port'      => config('consul.PORT'),
        'timeout'   => config('consul.TIMEOUT')
    ],
    'apollo' => [
        'host'    => config('Apollo.HOST'),
        'port'    => config('Apollo.PORT'),
        'appId'   => config('Apollo.APPID'),
        'timeout' => config('Apollo.TIMEOUT'),
    ],
    'lineFormatter'      => [
        'format'     => '%datetime% [%level_name%] [%channel%] [%event%] [tid:%tid%] [cid:%cid%] [traceid:%traceid%] [spanid:%spanid%] [parentid:%parentid%] %messages%',
        'dateFormat' => 'Y-m-d H:i:s',
    ],
    'noticeHandler'      => [
        'class'     => \Swoft\Log\Handler\FileHandler::class,
        'logFile'   => env('LOG_PATH').'/notice-%d{Y-m-d}.log',  // 2.0.6 支持日志按时间切割
        'formatter' => \bean('lineFormatter'),
        'levels'    => 'notice,info,debug,trace',
    ],
    'applicationHandler' => [
        'class'     => \Swoft\Log\Handler\FileHandler::class,
        'logFile'   =>  env('LOG_PATH').'/error-%d{Y-m-d}.log',
        'formatter' => \bean('lineFormatter'),
        'levels'    => 'error,warning',
    ],
    'logger'            => [
        'flushRequest' => false,
        'enable'       => false,
        'json'         => false,
        'handlers'     => [
            'application' => \bean('applicationHandler'),
            'notice'      => \bean('noticeHandler'),
        ],
    ],
    'httpServer'        => [
        'class'    => HttpServer::class,
        'port'     => config('application.HTTP_PORT' , 8002),
        'listener' => [
            'rpc' => bean('rpcServer')
//            'tcp' => bean('tcpServer'),
//             'ws' => bean('wsServer'),
        ],
        'process'  => [
//            'monitor' => bean(MonitorProcess::class)
//            'crontab' => bean(CrontabProcess::class)
        ],
        'on'       => [
//            SwooleEvent::TASK   => bean(SyncTaskListener::class),  // Enable sync task
            SwooleEvent::TASK   => bean(TaskListener::class),  // Enable task must task and finish event
            SwooleEvent::FINISH => bean(FinishListener::class)
        ],
        /* @see HttpServer::$setting */
        'setting' => [
            'task_worker_num'       => 12,
            'task_enable_coroutine' => true,
            'worker_num'            => 6,
            'package_max_length'    => 20971520,  //大约20M
            //支持https,签名文件
//            'ssl_cert_file' => '/my/certs/2288803_www.domain.com.pem',
//            'ssl_key_file'  => '/my/certs/2288803_www.domain.com.key',
        ]
    ],
    'httpDispatcher'    => [
        // Add global http middleware
        'middlewares'      => [
            \App\Http\Middleware\FavIconMiddleware::class,
            \Swoft\Http\Session\SessionMiddleware::class,
            // \Swoft\Whoops\WhoopsMiddleware::class,
            // Allow use @View tag
            \Swoft\View\Middleware\ViewMiddleware::class,
        ],
        'afterMiddlewares' => [
            \Swoft\Http\Server\Middleware\ValidatorMiddleware::class
        ]
    ],
    'db' => [
        'class'    => Database::class,
        'prefix'   => 'nhb_',
        'charset' => 'utf8mb4',
        'config'   => [
            'collation' => 'utf8mb4_unicode_ci',
            'strict'    => true,
            'timezone'  => '+8:00',
            'modes'     => 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES',
            'fetchMode' => PDO::FETCH_ASSOC,
        ],
        'writes' => [
            [
                'dsn'      => "mysql:dbname=nhb;host=172.25.0.21:8066",
                'username' => 'root',
                'password' => 'DRsXT5ZJ6Oi55LPQ'
            ]
        ],
        'reads'  => [
            [
                'dsn'      => "mysql:dbname=nhb;host=172.25.0.21:8066",
                'username' => 'root',
                'password' => 'DRsXT5ZJ6Oi55LPQ'
            ]
        ]
    ],
    'db.pool' => [
        'class'    => Pool::class,
        'database' => bean('db'),
    ],
    'db_oauth' => [
        'class'      => Database::class,
        'dsn'        => env('DB_OAUTH_DSN'),
        'username'   => env('DB_OAUTH_USERNAME'),
        'password'   => env('DB_OAUTH_PASSWORD'),
        'prefix'   => '',
        'charset' => 'utf8mb4',
        'config'   => [
            'collation' => 'utf8mb4_unicode_ci',
            'strict'    => true,
            'timezone'  => '+8:00',
            'modes'     => 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES',
            'fetchMode' => PDO::FETCH_ASSOC,
        ],
//        'dbSelector' => bean(DbSelector::class)
    ],
    'db_oauth.pool' => [
        'class'    => Pool::class,
        'database' => bean('db_oauth'),
    ],
    'db2'               => [
        'class'      => Database::class,
        'dsn'        => 'mysql:dbname=test2;host=127.0.0.1',
        'username'   => 'root',
        'password'   => 'swoft123456',
//        'dbSelector' => bean(DbSelector::class)
    ],
    'db2.pool' => [
        'class'    => Pool::class,
        'database' => bean('db2'),
    ],
    'db3'               => [
        'class'    => Database::class,
        'dsn'      => 'mysql:dbname=test2;host=127.0.0.1',
        'username' => 'root',
        'password' => 'swoft123456'
    ],
    'db3.pool'          => [
        'class'    => Pool::class,
        'database' => bean('db3')
    ],
    'migrationManager'  => [
        'migrationPath' => '@database/Migration',
    ],
    'redis'        => [
        'class'    => RedisDb::class,
        'host'     => config('redis.HOST'),
        'port'     => config('redis.PORT'),
        'password' => config('redis.PASSWORD'),
        'database' => 0,
        'option'   => [
            'prefix' => 'service-user:'
        ]
    ],
    'rpcServer'         => [
        'class' => ServiceServer::class,
        'port'  => config('application.RPC_PORT',8003)
    ],
    'wsServer'          => [
        'class'   => WebSocketServer::class,
        'port'    => config('application.WS_PORT',18307),
        'listener' => [
            'rpc' => bean('rpcServer'),
            // 'tcp' => bean('tcpServer'),
        ],
        'debug'   => config('swoft_debug'),
        'setting' => [
            // enable static handle
            'enable_static_handler'    => true,
            // swoole v4.4.0以下版本, 此处必须为绝对路径
            'document_root' => dirname(__DIR__) . '/public',
            'heartbeat_check_interval' => 5,
            'heartbeat_idle_time' => 10,
            'worker_num' => 6,
            'log_file' => alias('@runtime/swoole.log'),
            //task任务
            'task_worker_num'       => 12,
            'task_enable_coroutine' => true
        ],
        'on'      => [
            // 加上如下一行，开启处理http请求
            \Swoft\Server\SwooleEvent::REQUEST => bean(\Swoft\Http\Server\Swoole\RequestListener::class),
            // Enable task must task and finish event
            \Swoft\Server\SwooleEvent::TASK   => \bean(\Swoft\Task\Swoole\TaskListener::class),
            \Swoft\Server\SwooleEvent::FINISH => \bean(\Swoft\Task\Swoole\FinishListener::class)
        ]
    ],
     'wsConnectionManager' => [
         'storage' => bean('wsConnectionStorage')
     ],
     'wsConnectionStorage' => [
         'class' => \Swoft\Session\SwooleStorage::class,
     ],
    /** @see \Swoft\WebSocket\Server\WsMessageDispatcher */
    'wsMsgDispatcher' => [
        'middlewares' => [
            \App\WebSocket\Middleware\GlobalWsMiddleware::class
        ],
    ],
    /** @see \Swoft\Tcp\Server\TcpServer */
    'tcpServer'         => [
        'port'  => config('application.TCP_PORT',18308),
        'debug'   => config('swoft_debug'),
    ],
    /** @see \Swoft\Tcp\Protocol */
    'tcpServerProtocol' => [
        // 'type' => \Swoft\Tcp\Packer\JsonPacker::TYPE,
        'type' => \Swoft\Tcp\Packer\SimpleTokenPacker::TYPE,
        // 'openLengthCheck' => true,
    ],
    /** @see \Swoft\Tcp\Server\TcpDispatcher */
    'tcpDispatcher' => [
        'middlewares' => [
            \App\Tcp\Middleware\GlobalTcpMiddleware::class
        ],
    ],
    'cliRouter'         => [
        // 'disabledGroups' => ['demo', 'test'],
    ],
];
